package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.PromptManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/4 10:36
 * 邮箱：lanbuhan@163.com功能：更新密码
 */
@ContentView(R.layout.update_pass)
public class UpdatePasswordActivity extends BaseActivity implements TextWatcher {


    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.submit)
    TextView submit;

    @ViewInject(R.id.old_pass)
    EditText old_pass;

    @ViewInject(R.id.new_pass)
    EditText new_pass;

    @ViewInject(R.id.qure_pass)
    EditText qure_pass;


    @Event(value = {R.id.back, R.id.submit})
    private void viewOnClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
            case R.id.submit:
                submit.setEnabled(false);
                String newStr = getEditToString(new_pass);
                String qureStr = getEditToString(qure_pass);
                if (!newStr.equals(qureStr)) {
                    PromptManager.showToast(R.string.qure_pass_new);
                    return;
                }

                if (!CommonUtil.isPassNO(newStr)) {
                    PromptManager.showToast(R.string.pass_format_hint);
                    return;
                }

                int length = newStr.length();
                if (length < 6 || length > 16) {
                    PromptManager.showToast(R.string.password_number_hint);
                    return;
                }


                mapKeys.put(AppHttpKey.TYPE, "2");
                mapKeys.put(AppHttpKey.PASSWORD, newStr);
                mapKeys.put(AppHttpKey.OLD_PASSWORD, getEditToString(old_pass));
                AppHttpRequest.appQuitState(ApplicationConfig.UPDATE_USER_PWD,
                        mapKeys,
                        new AppHttpRequest.OnAppHttpListener() {
                            @Override
                            public void onAppHttpState(boolean isState, String result) {
                                if (isState) {
                                    PromptManager.showToast(R.string.update_pssword_sucess);
                                    UpdatePasswordActivity.this.finish();
                                }
                            }
                        });
                break;
        }
    }

    @Override
    public void initSucessView() {}

    @Override
    public void initData(Bundle savedInstanceState) {}

    @Override
    public void initViewHandler() {
        old_pass.addTextChangedListener(this);
        new_pass.addTextChangedListener(this);
        qure_pass.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        submit.setEnabled(false);
        submit.setBackgroundResource(R.drawable.ash_btn_frame);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (isEmptyOrHint(old_pass, R.string.old_pass_hint) &&
                isEmptyOrHint(new_pass, R.string.new_pass_hint) &&
                isEmptyOrHint(qure_pass, R.string.qure_pass_hint)) {
            submit.setEnabled(true);
            submit.setBackgroundResource(R.drawable.blue_btn_frame);
        } else {
            submit.setEnabled(false);
            submit.setBackgroundResource(R.drawable.ash_btn_frame);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
